package com.xinchao.ck_mips.utils;

import android.content.Context;
import android.content.Intent;

import com.orhanobut.logger.Logger;
import com.xinchao.devicemanage.constant.DeviceConstant;

import java.util.Calendar;
import java.util.Date;

/*
 *Created by xiaoqiang on 2018/8/30.
 */
public class HisiDeviceUtil {

    //1.重启
    public final static String ACTION_REBOOT = "android.intent.action.reboot";

    //2.关机
    public final static String ACTION_SHUTDOWN = "android.intent.action.shutdown";

    //3.休眠（关闭屏幕背光，网络保持连接）
    public final static String ACTION_GOTOSLEEP = "android.intent.action.gotosleep";

    //4.唤醒休眠（打开屏幕背光）
    public final static String ACTION_EXITSLEEP = "android.intent.action.exitsleep";

    //6.RTC时间更新
    public final static String RTC_TIME_UPDATE = "android.tvservice.intent.action.RTC_TIME_U PDATE";

    //8.APP静默安装
    public final static String ACTION_INSTALL_APP = "com.android.lango.installapp";

    //11.定时开关机
    public final static String SET_POWER_ON_OFF = "android.intent.action.setpoweronoff";


    /**
     * 重启
     *
     * @param context
     */
    public static void startUp(Context context) {
        Intent intent = new Intent(ACTION_REBOOT);
        sendBroadcast(context, intent);
    }

    /**
     * 休眠
     *
     * @param context
     */
    public static void goToSleep(Context context) {
        Intent intent = new Intent(ACTION_GOTOSLEEP);
        sendBroadcast(context, intent);
    }

    /**
     * 唤醒休眠
     *
     * @param context
     */
    public static void exitSleep(Context context) {
        Intent intent = new Intent(ACTION_EXITSLEEP);
        sendBroadcast(context, intent);
    }

    /**
     * 静默安装
     *
     * @param context
     * @param path    apk绝对路径
     */
    public static void installApp(Context context, String path) {
        Intent intent = new Intent(ACTION_INSTALL_APP);
        intent.putExtra("apppath", path);
        sendBroadcast(context, intent);
    }

    /**
     * 设置下一次定时开关机
     *
     */
    public static void setPowerOnOff(Context context, int on_h, int on_m, int off_h, int off_m, boolean enable) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(new Date());
        // 设置开始时间
        Calendar onTime = Calendar.getInstance();
        onTime.setTime(new Date());
        onTime.set(Calendar.HOUR_OF_DAY, on_h);
        onTime.set(Calendar.MINUTE, on_m);

        // 设置结束时间
        Calendar offTime = Calendar.getInstance();
        offTime.setTime(new Date());
        offTime.set(Calendar.HOUR_OF_DAY, off_h);
        offTime.set(Calendar.MINUTE, off_m);

        // 当前时间大于开机时间小于关机时间
        if (nowTime.after(onTime) && nowTime.before(offTime)) {
            onTime.add(Calendar.DATE, 1);
        }
        // 当前时间在关机时间之后
        else if (nowTime.after(offTime)) {
            // 开机时间天数+1
            onTime.add(Calendar.DATE, 1);
            // 关机时间天数+1
            offTime.add(Calendar.DATE, 1);
        }
        int[] timeonArray = new int[]{onTime.get(Calendar.YEAR),
                (onTime.get(Calendar.MONTH) + 1),
                onTime.get(Calendar.DAY_OF_MONTH),
                on_h,
                on_m};

        int[] timeoffArray = new int[]{offTime.get(Calendar.YEAR),
                (offTime.get(Calendar.MONTH) + 1),
                offTime.get(Calendar.DAY_OF_MONTH),
                offTime.get(Calendar.HOUR_OF_DAY),
                offTime.get(Calendar.MINUTE)};

        Intent intent = new Intent(SET_POWER_ON_OFF);
        intent.putExtra("timeon", timeonArray);
        intent.putExtra("timeoff", timeoffArray);
        intent.putExtra("enable", true);//使能开关机功能
        sendBroadcast(context, intent);
    }


    public static void sendBroadcast(Context context, Intent intent) {
        if (context != null) {
            context.sendBroadcast(intent);
        }
    }

}
